package component;

import com.fasterxml.jackson.core.JsonProcessingException;
import dao.EmployerDao;
import listener.ActionDoneListener;
import utils.ScreenUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

public class FindByDeptDialog extends JDialog {
    final int WIDTH = 400;
    final int HEIGHT = 360;
    private EmployerDao employerDao = new EmployerDao();
    private ActionDoneListener listener;
    public static String DEPT ;


    private JTable table;
    private Vector<String> titles;
    private Vector<Vector> tableData;
    private DefaultTableModel tableModel;
    public FindByDeptDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener){
        super(jf,title,isModel);
        this.listener  = listener;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();


        //组装部门
        Box deptBox = Box.createHorizontalBox();
        JLabel deptLabel = new JLabel("员工部门：");
        JRadioButton aBtn = new JRadioButton("人事 ",true);
        JRadioButton bBtn = new JRadioButton("管理",false);

        //实现单选的效果
        ButtonGroup bg4 = new ButtonGroup();
        bg4.add(aBtn);
        bg4.add(bBtn);

        deptBox.add(deptLabel);
        deptBox.add(Box.createHorizontalStrut(20));
        deptBox.add(aBtn);
        deptBox.add(bBtn);
        deptBox.add(Box.createHorizontalStrut(120));







        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("查询");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                DEPT =  bg4.isSelected(aBtn.getModel()) ? aBtn.getText() : bBtn.getText();
                dispose();
                try {
                    listener.done(null);
                } catch (JsonProcessingException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });


        btnBox.add(addBtn);


        vBox.add(Box.createVerticalStrut(15));
        vBox.add(deptBox);
        vBox.add(Box.createVerticalStrut(15));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(20));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(20));

        this.add(hBox);

    }



}
